const Koa = require('koa');
const render =require('koa-art-template');
const path= require('path');
//引入两个路由
const musicRouter = require('./router/music');
const userRouter = require('./router/user');
const serve = require('koa-static');
//创建服务器
const app=new Koa();
//渲染
render(app, {
  root: path.join(__dirname, 'view'),
  extname: '.html',
  debug: process.env.NODE_ENV !== 'production'
});
//中间件使用
app.use(userRouter.routes());
app.use(musicRouter.routes());
app.use(musicRouter.allowedMethods());

//为了给static重写url
app.use(async  (ctx,next) =>{
  if(ctx.url.startsWith('/public') )  {
    //重写url
    ctx.url=ctx.url.replace('/public','');
  }
   //放行
   await next();
})
//使用静态资源
//path.resolve('./public')对public下的东西进行处理
app.use(require('koa-static')(path.resolve('./public')));
app.listen(8880,()=>{
    console.log('启动了')
});
